package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(interfaceClass=MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;

    /**
     * 通过用户名字查找菜单
     * @param username
     * @return
     */
    @Override
    public List<Menu> findMenuList(String username) {
        //  1. 先查用户
        User userInDb= userDao.findUserByUsername(username);
        Integer userId=userInDb.getId();
//          2. 查看用角色
        Set<Role> roles=roleDao.findRoleById(userId);
        List<Menu> menus=null;
//        3. 查看主菜单
        for (Role role:roles){
            Integer roleId = role.getId();
            menus=menuDao.findMenuByRoleId(roleId);
            for (Menu menu:menus){
//                4.添加子菜单
                Integer id = menu.getId();
                List<Menu> child=menuDao.finMenuChildByparentMenuId(id);
                menu.setChildren(child);
            }
        }
        return menus;
    }
}
